//
//  APPLEHomeTableViewController.swift
//  123
//
//  Created by pangjiaxing on 15/12/27.
//  Copyright © 2015年 pangjiaxing. All rights reserved.
//

import UIKit
import SVProgressHUD
class APPLEHomeTableViewController: APPLEMainTableViewController {
    
    private let APPLECellReUseId = "APPLECellReUseId"
    private  lazy var appleStatusListViewModel :APPLEStatusListViewModel = APPLEStatusListViewModel()
    private lazy var pullUpView :UIActivityIndicatorView = {
        let indicator = UIActivityIndicatorView()
        // 指定风格
        indicator.activityIndicatorViewStyle = .WhiteLarge
        indicator.color = UIColor.purpleColor()
        // 指定大小
        indicator.sizeToFit()
        return indicator
    }()
    
    
    // 刷新控件
    private lazy var hmRefreshControl: HMRefreshControl = HMRefreshControl()
    
    
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        if userIslanding == false {
            
            visitorView.setupInfo(nil, messageText: nil)
            return
            
        }
        
        navigationItem.leftBarButtonItem=UIBarButtonItem(imageName: "navigationbar_friendsearch",  targat: nil, action: "")
        navigationItem.rightBarButtonItem=UIBarButtonItem(imageName: "navigationbar_pop", targat: self, action: "push")
        setUpTableView()
        loadData()
    }

    @objc private func push(){
        
        let  appletempviewcontroller = APPLETempViewController()
        navigationController?.pushViewController(appletempviewcontroller, animated: true)
        
    }
    
    private func setUpTableView(){
        tableView.registerClass(APPLEStatusCell.self, forCellReuseIdentifier: APPLECellReUseId)
        tableView.rowHeight = UITableViewAutomaticDimension
        tableView.estimatedRowHeight=200
        tableView.separatorStyle = .None
        tableView.backgroundColor=UIColor(white: 0.97, alpha: 1)
        tableView.tableFooterView=pullUpView
    //调用HMRefreshControl的一个方法
        tableView.addSubview(hmRefreshControl)
        hmRefreshControl.addTarget(self, action: "loadData", forControlEvents: UIControlEvents.ValueChanged)
    }
    
    @objc private func loadData(){
        
        appleStatusListViewModel.loadData(pullUpView.isAnimating()) { (isSecessed) -> () in
            if isSecessed {
                self.tableView.reloadData()
            }else{
                SVProgressHUD.showErrorWithStatus("请求失败")
            }
            self.endRefresh()
        }
    }
    
    private func endRefresh(){
        pullUpView.stopAnimating()
        hmRefreshControl.endRefreshing()
    }
    
}

extension APPLEHomeTableViewController {
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return appleStatusListViewModel.statuses?.count ?? 0
    }
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCellWithIdentifier(APPLECellReUseId, forIndexPath: indexPath) as!APPLEStatusCell
        cell.statusViewModel = appleStatusListViewModel.statuses![indexPath.row]
        return cell
    }
    
    // 将要显示某一个 cell 的时候会回调这个方法
    override func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
        
        // 判断如果是将要显示最后一行cell 的话就去执行加载数据的操作
        // pullUpView.isAnimating() --> 代表当前是不是正在上拉加载
        if indexPath.row == appleStatusListViewModel.statuses!.count - 1 && pullUpView.isAnimating() == false {
            print("去加载数据啦")
            // 开始动画
            pullUpView.startAnimating()
            // 加载数据
            loadData()
        }
    }

}
